<?php
defined( '_JEXEC' ) or die( 'Restricted Access' );
/**
 * @version     $Id$
 * @package     Virtuemart
 * @subpackage  GoodRelations Module
 * @copyright   Copyright (C) 2010 Alex Stolz - All rights reserved.
 * @license     LGPL
 */

$h2o = new h2o(dirname(__FILE__).'/templates/template-sitemap.xml', array("cache" => $cache, "cache_dir" => $cache_dir));

$sitemap_maxsize = 25000;
// render the page
if(($size = sizeof($sitemap_data['uris'])) > $sitemap_maxsize) {
	$sub_sitemap = array();
	$sitemap_uris = array();
	$sitemap_uris['uris'] = array();
	mkdir("sitemaps", 0777);
	if(is_dir("sitemaps")) {
		// create sitemaps
		for($i=0; $i<$size; $i+=$sitemap_maxsize) {
			$sitemap_uri = "sitemaps/sitemap$i.xml";
			$sub_sitemap['uris'] = array_slice($sitemap_data['uris'], $i, min($sitemap_maxsize, $size-$i));
			$sitemap_uris['uris'][] = BASE_URI.$sitemap_uri;
			$sitemap_file = fopen($sitemap_uri, "w+");
			if($sitemap_file) {
				// write contents into sitemap file
				fputs($sitemap_file, $h2o->render($sub_sitemap));
				fclose($sitemap_file);
			}
			else {
				echo "<br /><b>Error creating Sitemap files in Sitemap Index folder /sitemaps, please check write permissions.</b><br />";
				break;
			}
		}
	}
	else {
		echo "<br /><b>/sitemaps folder does not exist and could not be created in webserver directory, please create manually.</b><br />";
	}
	// create sitemap index
	$h2o = new h2o(dirname(__FILE__).'/templates/template-sitemapindex.xml');
	$sitemap_contents = $h2o->render($sitemap_uris);
}
else {
	// create sitemap
	$sitemap_contents = $h2o->render($sitemap_data);
}
?>